from micropython import const
import framebuf

_NOOP = const(0)
_DIGIT0 = const(1)
_DECODEMODE = const(9)
_INTENSITY = const(10)
_SCANLIMIT = const(11)
_SHUTDOWN = const(12)
_DISPLAYTEST = const(15)

class Matrix8x8:
	def __init__(self, spi, cs, num):
		self.spi = spi
		self.cs = cs
		self.cs.init(cs.OUT, True)
		self.buffer = bytearray(8 * num)
		self.num = num
		fb = framebuf.FrameBuffer(self.buffer, 8 * num, 8, framebuf.MONO_HLSB)
		self.framebuf = fb
		# Provide methods for accessing FrameBuffer graphics primitives. This is a workround
		# because inheritance from a native class is currently unsupported.
		# http://docs.micropython.org/en/latest/pyboard/library/framebuf.html
		self.fill = fb.fill  # (col)
		self.pixel = fb.pixel # (x, y[, c])
		self.hline = fb.hline  # (x, y, w, col)
		self.vline = fb.vline  # (x, y, h, col)
		self.line = fb.line  # (x1, y1, x2, y2, col)
		self.rect = fb.rect  # (x, y, w, h, col)
		self.fill_rect = fb.fill_rect  # (x, y, w, h, col)
		self.text = fb.text  # (string, x, y, col=1)
		self.scroll = fb.scroll  # (dx, dy)
		self.blit = fb.blit  # (fbuf, x, y[, key])
		self.init()

	def _write(self, command, data):
		self.cs(0)
		for m in range(self.num):
			self.spi.write(bytearray([command, data]))
		self.cs(1)

	def init(self):
		for command, data in (
			# (_NOOP, 0),
			(_SHUTDOWN, 0),
			(_DISPLAYTEST, 0),
			(_SCANLIMIT, 7),
			(_DECODEMODE, 0),
			(_INTENSITY, 7),
			(_SHUTDOWN, 1),
		):
			self._write(command, data)

	def brightness(self, value):
		if not 0 <= value <= 15:
			raise ValueError("Brightness out of range")
		self._write(_INTENSITY, value)

	def show(self):
		for y in range(8):
			self.cs(0)
			for m in range(self.num):
				self.spi.write(bytearray([_DIGIT0 + y, self.buffer[(y * self.num) + m]]))
			self.cs(1)
	

# import max7219
from machine import Pin, SPI
from utime import sleep

spi = SPI(1, baudrate=10000000, polarity=1, phase=0, sck=Pin(14), mosi=Pin(13))
ss = Pin(27, Pin.OUT)
display = Matrix8x8(spi, ss, 1)
# display.text('1234',0,0,1)
# display.show()
# display.text('1234',0,0,1)
# display.show()
# display.text('1234',0,0,1)
# display.show()
# display.text('1234',0,0,1)
# display.show()
display.fill(0)
display.text('ab',0,1)
display.show()


# for i in range(15):
# 	display.brightness(i)
# 	sleep(0.4)

# display.text('123 ',0,0,0)
# display.rect(13,2,3,3,0)
# display.clean()


# display.show()

# # display.pixel(24,7,0)
# display.pixel(17,0,1)
# display.pixel(16,0,1)
# display.pixel(15,6,1)
# display.pixel(14,5,1)
# display.pixel(13,4,1)
# display.pixel(30,6,1)
# display.pixel(1,1,1)
# display.pixel(2,2,1)
# display.pixel(3,3,1)
# display.pixel(4,4,1)
# print(display.buffer)

# display.show()
# display.brightness(15)
